package ru.bytedog.oiot.counter.bot.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ru.bytedog.oiot.counter.bot.entity.BotUser;
import ru.bytedog.oiot.counter.bot.entity.UserOtp;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface UserOtpRepository extends JpaRepository<UserOtp, Long> {
    @Query("select uo from UserOtp uo where :user = uo.user and :currentTime < uo.actualTime" +
            " and (uo.success is null or uo.success = false) order by uo.actualTime desc")
    List<UserOtp> getAllActiveCodes(@Param("currentTime") LocalDateTime currentTime, @Param("user") BotUser user);
}
